var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = e(require("../../@babel/runtime/regenerator")), s = require("../../@babel/runtime/helpers/asyncToGenerator"), i = require("../../utils/switchTab"), n = e(require("../../http/taskApi")), r = e(require("../../http/info")), o = e(require("../../miniprogram_npm/@vant/weapp/toast/toast")), l = require("../../utils/tool"), u = getApp(), h = new n.default(), c = new r.default();

Page({
    data: {
        singInfo: {},
        type: 1,
        dayTask: [],
        weekTask: [],
        achievement: [],
        rewards: {},
        show: !0,
        rules: "",
        isLogin: !1,
        skipPic: "",
        skipUrl: "",
        shareTitle: "".concat(u.global.loginInfo.nickname || "19八3", "问你今天签到了没"),
        shareUrl: "/pages/task/task",
        shareImg: "http://newimg.ap1983.com/prod/20210911/80c11b03f0594bd582d822d0c5f8497c.png",
        nickname: ""
    },
    onLoad: function() {
        this.getRule();
    },
    onShow: function() {
        (0, i.switchToTab)(this, 3), this.getData(), this.getTaskList(), u.global.isLogin() && this.getWishValue(), 
        this.setData({
            isLogin: u.global.isLogin()
        }), this.selectComponent("#sharePop").onClose();
    },
    getData: function() {
        var e = this;
        return s(a.default.mark(function t() {
            var s, i;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, wx.showLoading({
                        title: "加载中",
                        mask: !0
                    }), s = [ h.signList(), h.signRewards() ], t.next = 5, Promise.allSettled(s);

                  case 5:
                    i = t.sent, e.setSingList(i[0], i[1]), wx.hideLoading(), t.next = 13;
                    break;

                  case 10:
                    t.prev = 10, t.t0 = t.catch(0), wx.hideLoading();

                  case 13:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 10 ] ]);
        }))();
    },
    setSingList: function(e, t) {
        if ("fulfilled" == e.status && "fulfilled" == t.status) {
            var a = e.value.data;
            Object.assign(a, t.value.data), a.isSingIn = (0, l.isToday)(a.lastSignTime), this.setData({
                singInfo: a,
                shareTitle: t.value.data.shareTitle,
                shareUrl: "/pages/task/task",
                shareImg: t.value.data.sharePic
            });
        } else this.setData({
            singInfo: {}
        });
    },
    singIn: function() {
        var e = this;
        wx.showLoading({
            title: "加载中"
        }), h.signIn().then(function(t) {
            wx.hideLoading(), (0, o.default)({
                selector: "#sign-toast",
                duration: "3000",
                mask: !0
            }), e.setData({
                rewards: t.data
            }), e.getData(), e.getWishValue();
        }).catch(function(e) {
            wx.hideLoading(), (0, l.toastToPage)(e.msg);
        });
    },
    getTaskList: function() {
        var e = this, a = this.data.type;
        h.taskList(a).then(function(s) {
            var i, n = t(s.data);
            try {
                for (n.s(); !(i = n.n()).done; ) {
                    var r = i.value;
                    r.percentage = r.schedule / r.ultimate * 100;
                }
            } catch (e) {
                n.e(e);
            } finally {
                n.f();
            }
            1 == a ? e.setData({
                dayTask: s.data
            }) : 2 == a ? e.setData({
                weekTask: s.data
            }) : e.setData({
                achievement: s.data
            });
        }).catch(function(e) {
            (0, l.toastToPage)(e.msg);
        });
    },
    changeTask: function(e) {
        this.setData({
            type: e.detail.name
        }), this.getTaskList();
    },
    getRule: function() {
        var e = this;
        return s(a.default.mark(function t() {
            var s;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, h.signRule();

                  case 2:
                    s = t.sent, e.setData({
                        rules: s.msg
                    });

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    showRules: function() {
        this.selectComponent("#rulesPop").showPopup();
    },
    showImagePop: function(e) {
        var t = this;
        return s(a.default.mark(function s() {
            var i, n, r, o, c;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (i = e.detail.task, n = i.skipType, r = i.skipPic, o = i.skipUrl, 3 != n || !o.startsWith("http")) {
                        a.next = 11;
                        break;
                    }
                    if ("112313987809804288" != i.taskAchievementId) {
                        a.next = 10;
                        break;
                    }
                    return a.next = 8, (0, l.getWechatLoginCode)();

                  case 8:
                    c = a.sent, h.joinGroup(c);

                  case 10:
                    u.global.loginThenRedirectTo("/pages/webview/webview?url=".concat(o));

                  case 11:
                    if (3 != n || o.startsWith("http") || u.global.loginThenRedirectTo(o), 0 != n && 1 != n || (t.setData({
                        skipPic: r
                    }), o && t.setData({
                        skipUrl: o
                    }), t.selectComponent("#imagePop").showPopup()), 2 != n) {
                        a.next = 19;
                        break;
                    }
                    if (u.global.isLogin()) {
                        a.next = 17;
                        break;
                    }
                    return u.global.loginThenRedirectTo(), a.abrupt("return");

                  case 17:
                    t.setData({
                        shareTitle: i.shareTitle,
                        shareUrl: i.skipUrl,
                        shareImg: i.sharePic,
                        skipButtom: i.skipButtom,
                        skipPic: r
                    }), t.selectComponent("#sharePop").showPopup();

                  case 19:
                  case "end":
                    return a.stop();
                }
            }, s);
        }))();
    },
    getWishValue: function() {
        var e = this;
        return s(a.default.mark(function t() {
            var s;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, c.getUmsMemberInfo();

                  case 2:
                    s = t.sent, e.setData({
                        wishValue: s.data.availableWish,
                        nickname: s.data.nickname
                    });

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    onShareAppMessage: function(e) {
        var t;
        return "button" == e.from && "share" == e.target.dataset.type ? (this.data.shareUrl && (t = "/pages/share-middle/share-middle?redirectTo=".concat(encodeURIComponent(this.data.shareUrl)) + "&shareFrom=" + u.getUsername()), 
        {
            title: this.data.shareTitle,
            path: t,
            imageUrl: this.data.shareImg
        }) : "button" == e.from && "sign" == e.target.dataset.type ? {
            title: this.data.shareTitle,
            path: "/pages/task/task?shareFrom=" + u.getUsername(),
            imageUrl: this.data.shareImg
        } : {
            title: "线上摇盲盒，让潮玩更好玩",
            path: "/pages/index/index?shareFrom=" + u.getUsername()
        };
    }
});